Thread: Matrix Question

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    5

    Matrix Question

    Hello to the wonderful world of programmers
    I am having a great deal trouble with a matrix/arrays.

    I need to set up a user defined array n by m
    and then get the use to enter values into it, but i cant make any progress, so far i have (taken mostly for other threads on this board)

    Code:
    #include <stdio.h>
    
    int main()
    {
      int nSIZE = 1;
      double *ptr;
      int i,j;
      while(nSIZE != 0)
        {
          printf("enter size : \n");
          scanf("%d", &nSIZE);
    
                ptr = (double *)malloc(sizeof(double)*nSIZE*nSIZE);
    
    	
    printf("please enter each value of the matrix: \n");
          for(i=0;i<nSIZE;i++)
    	{
    	  for(j=0;j<nSIZE;j++)
    	    {
    			
    		scanf( "%d", &ptr[i] );
    		(ptr[i]) = j;
    		printf("%d", (ptr[i]));
    	    }
    	  printf("\n");
    	}
    	  for(i=0;i<nSIZE;i++)
    		  	{
    	  for(j=0;j<nSIZE;j++)
    	  {
    	  printf("%d", ptr[i][j]);
    	  }
          free(ptr);
        }
    }
    Thanks for any help!!

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You need ptr to be of type double** . Look at it like a pointer to an array of pointers. The array of pointers are the rows in your matrix.

    So you need to allocate memory for n double* and then for each of these m doubles.

    Then you free in reverse order, you free each of the row pointers and at the end the pointer to the array of rows.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The way you are doing it is valid. You've got a single dimension array of nxn elements and you are placing values in it sequentially.

    To print it out you'd need to access pnt[i * nSize + j].

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Code:
    #include <stdio.h>
    
    int main()
    {
      int nSIZE = 1;
      double **ptr;
      int i,j;
      while(nSIZE != 0)
        {
          printf("enter size : \n");
          scanf("%d", &nSIZE);
    
          ptr = malloc(nSIZE*sizeof*ptr);
          for(i=0;i<nSIZE;++i) ptr[i]=malloc(nSIZE*sizeof**ptr);
    
    	
    printf("please enter each value of the matrix: \n");
          for(i=0;i<nSIZE;i++)
    	{
    	  for(j=0;j<nSIZE;j++)
    	    {			
    		scanf( "%lf", &ptr[i][j] );
    	    }
    	  printf("\n");
    	}
    	  for(i=0;i<nSIZE;i++)
    	  for(j=0;j<nSIZE;j++)
    	  {
    	  printf("%f", ptr[i][j]);
    	  }
          for(i=0;i<nSIZE;++i) free(ptr[i]);
          free(ptr);
        }
    }

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by BillyTKid View Post
    Code:
    #include <stdio.h>
    
    
    
          ptr = malloc(nSIZE*sizeof*ptr);
          for(i=0;i<nSIZE;++i) ptr[i]=malloc(nSIZE*sizeof**ptr);
    
    
    }
    You have to type cast malloc because it's of type void.

    Code:
     ptr = (double **) malloc(nSIZE*sizeof(double *));
     for(i=0;i<nSIZE;++i) ptr[i]= (double *) malloc(nSIZE*sizeof(double) );
    Also, you will have to include "stdlib.h" because that header file contain prototype for malloc.
    Last edited by nimitzhunter; 12-02-2010 at 11:39 PM.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by nimitzhunter View Post
    You have to type cast malloc because it's of type void.
    No, you don't and you shouldn't.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by nimitzhunter View Post
    You have to type cast malloc because it's of type void.
    Only reason you would want to do that in C is to make the code compatible with C++, which does require explicit typecast from void*. On the other hand, you shouldn't be using malloc() and friends in C++; new and delete is the C++ Way.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Music Programming - Serial Matrix Display (Help needed)
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-13-2007, 04:28 PM
  2. Music Programming - Serial Matrix Display
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 04:16 PM
  3. Question about view vector, position, and matrix speed
    By Silvercord in forum Game Programming
    Replies: 1
    Last Post: 02-03-2003, 12:37 PM
  4. Help!! Tips on Matrix Calculator Program please!
    By skanxalot in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2002, 11:26 AM